package com.antt.cn.test3.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Ant on 2017/5/2  18:24
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class BsrView1 extends View {

    private boolean isInit = false;

    private Path mPath;
    private Paint mPaint;

    private Point[] points;


    public BsrView1(Context context) {
        super(context);
        initThisView(context, null, 0);
    }

    public BsrView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs, 0);
    }

    public BsrView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(3.0f);
        isInit = true;
    }

    public void setPoints(Point[] points) {
        this.points = points;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!isInit)
            return;
        drawLinear(canvas);

    }


    public void drawLinear(Canvas canvas) {
        if (points == null)
            return;

        mPath.reset();

        for(int i=0,n=points.length;i<n;i++){
            Point p = points[i];
            mPath.quadTo(p.x+0.0f,p.y+0.0f,p.y+0.0f,p.x+0.0f);
        }

        canvas.drawPath(mPath, mPaint);
        invalidate();
    }
}
